from rest_framework import serializers
from .models import Equipment


class EquipmentSerializer(serializers.ModelSerializer):
    """
    设备资源序列化器
    """
    class Meta:
        model = Equipment
        fields = ['id', 'organization', 'device_name', 'device_quantity', 'single_device_capacity', 'device_capacity', 'created_at', 'updated_at']
        read_only_fields = ['id', 'created_at', 'updated_at']

    def validate_device_quantity(self, value):
        """
        验证设备数量必须为正数
        """
        if value <= 0:
            raise serializers.ValidationError("设备数量必须大于0")
        return value

    def validate_single_device_capacity(self, value):
        """
        验证单设备容量必须为正数
        """
        if value <= 0:
            raise serializers.ValidationError("单设备容量必须大于0")
        return value